package com.ipad.ui.fragmen;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;

import com.ipad.R;
import com.ipad.utils.LogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ChoosTimeFragment extends DialogFragment {
    @BindView(R.id.start_time)
    DatePicker startTime;
    @BindView(R.id.end_time)
    DatePicker end_time;
    @BindView(R.id.cancle_time)
    Button cancle_time;
    @BindView(R.id.confirm_time)
    Button confirm_time;
    SelectTime selectTime;

    String start_str="";
    String end_str="";
    public ChoosTimeFragment() {
        // Required empty public constructor
    }

    public static ChoosTimeFragment newInstance() {
        
        Bundle args = new Bundle();
        
        ChoosTimeFragment fragment = new ChoosTimeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.item_time_dialog, container, false);
        ButterKnife.bind(this,inflate);
        init();
        return inflate;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setWindowAnimations(R.style.windowAnimotion);
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(R.style.img_loading_style,R.style.img_loading_style);
    }

    private void init() {
        selectTime = (SelectTime) getActivity();
        startTime.updateDate(startTime.getYear(), startTime.getMonth(), 01);
        end_time.updateDate(startTime.getYear(), startTime.getMonth(), 01);
        start_str = startTime.getYear()+"-"+(startTime.getMonth()+1)+"-01";
        end_str = end_time.getYear()+"-"+(end_time.getMonth()+1)+"-01";
        LogUtils.e("chooseTime",start_str+"//"+end_str);


        startTime.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                start_str = startTime.getYear()+"-"+(startTime.getMonth()+1)+"-"+startTime.getDayOfMonth();
            }
        });
        end_time.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                end_str = end_time.getYear()+"-"+(end_time.getMonth()+1)+"-"+end_time.getDayOfMonth();

            }
        });


        cancle_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        confirm_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startTime
                selectTime.selectTime(start_str,end_str);
                dismiss();
            }
        });

    }
    public interface SelectTime{
         void selectTime(String start_time,String end_time);
    }
}
